package cn.yhr.summerboot.core.warpper;

import cn.yhr.summerboot.core.util.Assert;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.lang.reflect.Field;

/**
 * @Tittle BeanWarpperImpl
 * @Description
 * @Author yhr
 * @Date 2018/11/28 15:33
 * @Version 1.0
 */
public class BeanWarpperImpl implements BeanWarpper {

    private static final Logger logger = LoggerFactory.getLogger(BeanWarpperImpl.class);

    private Object instance;

    public BeanWarpperImpl(Object instance){
        setWrapperInstance(instance);
    }

    private void setWrapperInstance(Object instance) {
        Assert.notNull(instance, "Bean object must not be null");
        this.instance=instance;
    }

    @Override
    public void setPropertyValue(String propertyName, Object object) {
        try {
            Field field = instance.getClass().getDeclaredField(propertyName);
            field.setAccessible(true);
            field.set(instance,object);
        } catch (Exception e) {
            logger.error(e.getMessage());
            e.printStackTrace();
        }
    }
}
